package com.cyanite.playlv2.widget;

import com.cyanite.playlv2.R;
import com.cyanite.playlv2.util.RemoteResourceManager;
import com.cyanite.playlv2api.types.Task;
import com.cyanite.playlv2api.util.ConstantsUtils.ShareListAdapterConsts;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

public class ShareListAdapter extends BaseTaskAdapter implements ObservableAdapter {

    private int mLayoutToInflate;
    private LayoutInflater mInflater;
    private int mShowWay;
    private CharSequence mPrefix1;
    private CharSequence mPrefix2;
    private CharSequence mPrefix3;
    private RemoteResourceManager mRrm;
    private RemoteResourceManagerObserver mResourcesObserver;
    private Handler mHandler = new Handler();
    
    public ShareListAdapter(Context context, int showWay, CharSequence prf1, CharSequence prf2, CharSequence prf3) {
        super(context);
        mInflater = LayoutInflater.from(context);
        mLayoutToInflate = R.layout.share_list_item;
        mShowWay = showWay;
        mPrefix1 = prf1;
        mPrefix2 = prf2;
        mPrefix3 = prf3;
    }

    public void removeObserver() {
        mHandler.removeCallbacks(mPhoto);
        mRrm.deleteObserver(mResourcesObserver);
    }
    
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView( int position, View convertView, ViewGroup parent)   {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(mLayoutToInflate, null);
            holder = new ViewHolder();
            holder.photo = (ImageView) convertView.findViewById(R.id.ivSharePhoto);
            holder.line1 = (TextView)convertView.findViewById(R.id.tvFirstLine);
            holder.line1.setVisibility(View.GONE);
            holder.line2 = (TextView)convertView.findViewById(R.id.tvSecondLine);
            holder.line2.setVisibility(View.GONE);
            holder.line3 = (TextView)convertView.findViewById(R.id.tvThirdLine);
            holder.line3.setVisibility(View.GONE);
            holder.line4 = (TextView)convertView.findViewById(R.id.tvFouthLine);
            holder.line4.setVisibility(View.GONE);
            holder.line5 = (TextView)convertView.findViewById(R.id.tvFifthLine);
            holder.line5.setVisibility(View.GONE);
            holder.line6 = (TextView)convertView.findViewById(R.id.tvSixthLine);
            holder.line6.setVisibility(View.GONE);
            convertView.setTag(holder);
        }  else  {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.line1.setVisibility(View.VISIBLE);
        holder.line2.setVisibility(View.VISIBLE);
        holder.line3.setVisibility(View.VISIBLE);

        Task task = (Task) getItem(position);
        if (task.getPhoto() != null && !task.getPhoto().equals("")) {
            Uri photoUri = Uri.parse(task.getPhoto());
            if (!mRrm.exists(photoUri)) {
                mRrm.request(photoUri);
            }
        }
        holder.line6.setText(task.getPostDate());
        holder.line6.setVisibility(View.VISIBLE);
        switch( mShowWay )  {
            case ShareListAdapterConsts.SHARE_LIST_ADAPTER_COMMON:
                holder.line1.setText(task.getInfo().getInfo1());
                holder.line2.setText(mPrefix2+": "+ task.getInfo().getInfo2());
                holder.line3.setText(task.getInfo().getInfo3());
                break;
            case ShareListAdapterConsts.SHARE_LIST_ADAPTER_TRAFFIC:
                holder.line4.setVisibility(View.VISIBLE);
                holder.line1.setText(task.getInfo().getInfo1());
                holder.line2.setText(mPrefix1+": "+ task.getInfo().getInfo2());
                holder.line3.setText(mPrefix2+": "+ task.getInfo().getInfo3());
                holder.line4.setText(task.getInfo().getInfo3());
                break;
            case ShareListAdapterConsts.SHARE_LIST_ADAPTER_NEAR:
                holder.line4.setVisibility(View.VISIBLE);
                holder.line5.setVisibility(View.VISIBLE);
                holder.line1.setText(task.getInfo().getInfo1());
                holder.line2.setText(mPrefix1+": "+task.getInfo().getInfo2());
                holder.line3.setText(mPrefix2+": "+task.getInfo().getInfo3());
                holder.line4.setText(mPrefix3+": "+task.getInfo().getInfo4());
                holder.line5.setText(task.getInfo().getInfo5());
                break;
            default:
                break;
        }
        if (task.getPhoto() != null && !task.getPhoto().equals("")) {
            final Uri photoUri = Uri.parse(task.getPhoto());
            try {
                Bitmap bitmap = BitmapFactory.decodeStream(mRrm.getInputStream(photoUri));
                task.setPhotoBitmap(bitmap);
                holder.photo.setImageBitmap(bitmap);
            } catch (IOException e) {
                holder.photo.setImageResource(R.drawable.trip_default_photo);
            }
        }   else {
            holder.photo.setImageResource(R.drawable.trip_default_photo);
        }
        return convertView;
    }

    private class RemoteResourceManagerObserver implements Observer {
        @Override
        public void update(Observable observable, Object data) {
            mHandler.post(mPhoto);
        }
    }
    
    private Runnable mPhoto = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };
    
    static class ViewHolder {
        TextView line1;
        TextView line2;
        TextView line3;
        TextView line4;
        TextView line5;
        TextView line6;
        ImageView photo;
    }

}
